package com.zxy.salenet.web.service.imple;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zxy.salenet.web.mapper.SalenetCommMapper;
import com.zxy.salenet.web.mapper.SalenetGoodsMapper;
import com.zxy.salenet.web.mapper.SalenetIndentMapper;
import com.zxy.salenet.web.mapper.SalenetUserMapper;
import com.zxy.salenet.web.pojo.SalenetCar;
import com.zxy.salenet.web.pojo.SalenetComm;
import com.zxy.salenet.web.pojo.SalenetGoods;
import com.zxy.salenet.web.pojo.SalenetIndent;
import com.zxy.salenet.web.pojo.vo.BuySuccessVO;
import com.zxy.salenet.web.service.LoadCarData;
import com.zxy.salenet.web.service.UserOperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author: zxy
 * @version: 1.0
 * @description: 用户获取商品实现类
 */
    /**
 * UserOperationServiceImpl
 */
@Service
public class UserOperationServiceImpl implements UserOperationService {
    @Autowired
    private SalenetGoodsMapper goodsMapper;
    @Autowired
    private SalenetUserMapper userMapper;
    @Autowired
    private SalenetCommMapper commMapper;
    @Autowired
    private SalenetIndentMapper indentMapper;
    /**
     * 加密盐
     */
    private String salt = "zxy";

    /**
     * 获取商品 根据名称
     *
     * @param goodsName 商品名称
     * @param pageNum   当前页
     * @param pageSize  总共显示数据数量
     * @return
     */
    @Override
    public PageInfo<SalenetGoods> queryGoodsByName(String goodsName, Integer pageNum, Integer pageSize) {
        goodsName = "%" + goodsName + "%";
        // TODO: 2021/3/4  获取商品信息
        PageHelper.startPage(pageNum, pageSize);
        List<SalenetGoods> allGoodsList = goodsMapper.SelectAllGoodsByName(goodsName);
        PageInfo<SalenetGoods> allPage = new PageInfo<>(allGoodsList);
        return allPage;
    }

    /**
     * 获取商品 根据类别
     *
     * @param goodsType 商品类型编号
     * @param pageNum   当前页
     * @param pageSize  一页显示数据数量
     * @return
     */
    @Override
    public PageInfo<SalenetGoods> queryGoodsByGoodsType(Integer goodsType, Integer pageNum, Integer pageSize) {
        // TODO: 2021/3/4 获取商品信息
        PageHelper.startPage(pageNum, pageSize);
        List<SalenetGoods> allGoodsList = goodsMapper.SelectAllGoodsByType(goodsType);
        PageInfo<SalenetGoods> allPage = new PageInfo<>(allGoodsList);
        return allPage;
    }

    /**
     * 商品购买
     *
     * @return
     */
    @Override
    public BuySuccessVO buyGoods(List<SalenetCar> car, Integer getAddress) {
//        //首先验证支付密码是否正确
//        payPass = DigestUtils.md5DigestAsHex((salt + payPass).getBytes());
//        Integer userPayTrueOrFalse = userMapper.selectPayPassTrue(payPass, car.get(0).getCarGoodsUserId());
//        if (userPayTrueOrFalse != 1) {
//            BuySuccessVO buySuccessVO = new BuySuccessVO();
//            buySuccessVO.setErrorCode(101);
//            return buySuccessVO;
//        }
        //对商品的数量进行修改 如果不够返回商品不足信息提示
        BuySuccessVO buySuccessVO = new BuySuccessVO();
        Integer indentId = new Random().nextInt(2000) + 1987;
        System.out.println("当前list"+car);
        //获取当前商品的数量
        for (SalenetCar cars : car) {
            SalenetGoods currGoodsMessage = goodsMapper.queryGoodsCount(cars.getCarGoodsId());
            Integer buyCount = currGoodsMessage.getGoodsCount() - cars.getCarGoodsCount();
            if (currGoodsMessage.getGoodsCount() - cars.getCarGoodsCount() < 0) {
                 buySuccessVO = new BuySuccessVO();
                buySuccessVO.setErrorCode(102);
                return buySuccessVO;
            }
            buySuccessVO = new BuySuccessVO();
            //如果数量足够
            //然后根据传过来的数据生成返回界面所需要的数据
            //随机生成订单码
//            Integer goodsCode = new Random().nextInt(50000) + 10 - 9 + 3;
            buySuccessVO.setBuyId(indentId);
            //计算总价格 数量乘以价格
            BigDecimal currCount = currGoodsMessage.getGoodsPrice().multiply(new BigDecimal(cars.getCarGoodsCount()));
            buySuccessVO.setPayMoney(currCount);
            // TODO: 2021/3/6
            //设置日期
            buySuccessVO.setPayTime(new Date());
            //设置商品名称
            buySuccessVO.setBuyName(buySuccessVO.getBuyName());
            //获取收货地址 省市县加具体地址拼接
            SalenetComm address = commMapper.selectCommById(getAddress);
            String getGoodsAddress = address.getCommUsername() + " " + address.getCommProvince() + " " + address.getCommCity() + " " + (address.getCommCounty() + address.getCommVillage() == null ? "" : address.getCommVillage()) + " " + address.getCommAddress();
            buySuccessVO.setGetAddress(getGoodsAddress);
            //设置单价
            buySuccessVO.setOneGoodsMoney(currGoodsMessage.getGoodsPrice());
            //设置错误码
            buySuccessVO.setErrorCode(null);
            //给订单表添加数据
            SalenetIndent salenetIndent = new SalenetIndent();
            // TODO: 2021/3/6
            //添加到订单购买中去
            //设置用户下单对应的下单码
            System.out.println("当前下单码"+indentId);
            salenetIndent.setIndentId(indentId);
            //设置用户下单的id
            salenetIndent.setIndentUser(cars.getCarGoodsUserId());
            //设置用户下单时间
            salenetIndent.setIndentTimes(new Date());
            //设置用户支付时间
            salenetIndent.setIndentPayTimes(new Date());
            //设置发货时间  一般默认为null
            salenetIndent.setIndentSendTimes(null);
            //设置收货地址
            salenetIndent.setIndentAddress(getAddress);
            //代表待发货
            salenetIndent.setIndentStatus(5);
            //设置订单正产显示
            salenetIndent.setIndentLife(1);
            salenetIndent.setIndentName(cars.getCarGoodsName());
            //设置支付金额
            salenetIndent.setIndentPrice(buySuccessVO.getPayMoney());
            //用户购买的对应商品的编号
            salenetIndent.setIndentPayName(cars.getCarGoodsId());
            //设置用户购买的数量
            salenetIndent.setIndentPayCount(cars.getCarGoodsCount());
            salenetIndent.setImgUrl(goodsMapper.selectImgUrl(cars.getCarGoodsId()));
//            Integer addGoodsResult = indentMapper.addIndent(salenetIndent);
            Integer addGoodsResult = indentMapper.insertSelective(salenetIndent);
            //如果添加失败的话则返回支付成功信息设置为509
            if (addGoodsResult == 0) {
                buySuccessVO.setErrorCode(509);
            } else {
                //成功后修改商品的库存数量
                Integer goodsId = currGoodsMessage.getGoodsId();
                goodsMapper.updateGoodsCounts(buyCount, currGoodsMessage.getGoodsId());
//                //购买成功后移除商品
//                car.remove(cars);
            }
        }
        return buySuccessVO;
    }

    /**
     * 获取对应用户id的购物车信息
     *
     * @param userId
     * @return
     */
    @Override
    public List<SalenetCar> queryCarById(Integer userId) {
        //获取购物车map集合
        Map<Integer, List<SalenetCar>> carMap = LoadCarData.carMap;
        List<SalenetCar> salenetCarList = new ArrayList<>();
        //循环获取信息
        salenetCarList = carMap.get(userId);
        //获取对应用户的数据
        return salenetCarList;
    }

    /**
     * 更改商品信息
     *
     * @param userId
     * @param goodsId
     * @param currCount
     * @return
     */
    @Override
    public Integer updateGoodsCount(Integer userId, Integer goodsId, Integer currCount) {
        Map<Integer, List<SalenetCar>> carMap = LoadCarData.carMap;
        List<SalenetCar> salenetCarList = carMap.get(userId);
        for (SalenetCar salenetCar : salenetCarList) {
            //对当前商品进行修改
            if (salenetCar.getCarGoodsId().equals(goodsId) && salenetCar.getCarGoodsUserId().equals(userId)) {
                //修改当前商品数量
                //获取当前数量
                Integer currNum = salenetCar.getCarGoodsCount();
                if (currNum > currCount) {
                    //当前减少商品
                    //修改商品总价
                    BigDecimal updatePriceCount = salenetCar.getCarGoodsPrice().multiply(new BigDecimal(salenetCar.getCarGoodsCount() - (salenetCar.getCarGoodsCount() - currCount)));
                    //修改总价
                    salenetCar.setCarGoodsPricecount(updatePriceCount);

                } else if (currNum < currCount) {
                    //当前增加商品
                    //修改商品总价
                    BigDecimal updatePriceCount = salenetCar.getCarGoodsPrice().multiply(new BigDecimal(currCount));
                    //修改总价
                    salenetCar.setCarGoodsPricecount(updatePriceCount);
                }
                salenetCar.setCarGoodsCount(currCount);
            }
        }
        System.out.println(carMap);
        return 1;
    }

    /**
     * 删除购物车中的信息
     *
     * @param userId
     * @param goodsId
     * @return
     */
    @Override
    public Integer deleteGoodsForCarByGoodsIdAndUserId(Integer userId, Integer goodsId) {
        Map<Integer, List<SalenetCar>> carMap = LoadCarData.carMap;
        List<SalenetCar> salenetCarList = carMap.get(userId);
        int index = 0;
        for (int i = 0; i < salenetCarList.size(); i++) {
            if (goodsId.equals(salenetCarList.get(i).getCarGoodsId()) && userId.equals(salenetCarList.get(i).getCarGoodsUserId())) {
                index = i;
            }
        }
        //移除对应的商品信息
        salenetCarList.remove(index);
        System.out.println(carMap);
        return null;
    }

    /**
     * 添加商品的购物车
     *
     * @param car
     * @return
     */
    @Override
    public Integer addGoodsToCar(Integer userId, SalenetCar car) {
        Map<Integer, List<SalenetCar>> carMap = LoadCarData.carMap;
        System.out.println(carMap);
        List<SalenetCar> salenetCarList = carMap.get(userId);
        int isLive = 0;
        if (salenetCarList.size() > 0) {
            for (SalenetCar car1 : salenetCarList) {
                if (car1.getCarGoodsId().equals(car.getCarGoodsId())) {
                    car1.setCarGoodsCount(car.getCarGoodsCount() + car1.getCarGoodsCount());
                    car1.setCarGoodsPricecount(car.getCarGoodsPricecount().add(car1.getCarGoodsPricecount()));
                    System.out.println("修改成功");
                    isLive++;
                }
            }
        }
        if (isLive == 0) {
            car.setId(new Random().nextInt(100) + 10);
            System.out.println("当前添加商品数据" + car);
            salenetCarList.add(car);
        }
        return 0;
    }

    /**
     * 验证是否为正品
     *
     * @param goodsId
     * @param userId
     * @param code
     * @return
     */
    @Override
    public Integer queryTrueAndFalse(Integer goodsId, Integer userId, Integer code) {
        Integer verify = goodsMapper.selectTrueAndFalse(goodsId, code);
        return verify;
    }

    /**
     * 获取对应商品信息
     *
     * @param goodsId
     * @return
     */
    @Override
    public SalenetGoods queryGoodsById(Integer goodsId) {
        return goodsMapper.selectGoodByGoodsId(goodsId);
    }
}
